<?php


/** ******************************
 * @author  :   Yêu Tinh
 * @email   :   domain.web.online@gmail.com
 * @since   :   8-07-2013
 *********************************/
 
class UsersController extends AppController {

    public $name = 'Users';
    public $uses = array('User');
    public $type = 'khachhang';
    
    public function beforeFilter() {
        parent::beforeFilter();
        if(in_array($this->request->params['action'],array('index','search'))) $this->save_url();
    }

    public function index() {
        $this->paginate = array(
            'order' => array('User.point' => 'DESC'),
            'limit' => '150'
        );
        
        $this->set('view', $this->paginate('User',array()));	
    }
    
    public function search(){
        if ($this->request->is('post')) {
            $this->Session->write('search_keyword', $this->request->data['search_keyword']);
        }
        $search_keyword = $this->Session->read('search_keyword');
        
        $this->paginate = array(
            'conditions' => array(
                'User.email LIKE' => '%'.$search_keyword.'%'
            ),
            'order' => array('User.point' => 'DESC'),
            'limit' => '150'
        );
        //pr($this->paginate('User',array())); die;
        $this->set('view', $this->paginate('User',array()));	
        $this->render('index');
    }
    
    public function congdiem($id = null){
        if (!empty($this->request->data)) {
            if(!empty($this->request->data['User']['point']) && !empty($id)){
                if(is_numeric($this->request->data['User']['point'])){
                    $user = $this->User->findById($id);
                    $this->User->id = $id;
                    $this->User->saveField('point', ($user['User']['point'] + $this->request->data['User']['point']));
                    
                    $this->loadModel('Cmtaction');
                    $this->Cmtaction->create();
                    $data = array();
                    $data['Cmtaction']['foreign_id'] = $id;
                    $data['Cmtaction']['user_id'] = $_SESSION['id'];
                    $data['Cmtaction']['type'] = $this->type;
                    $data['Cmtaction']['content'] = 'Cộng '.$this->request->data['User']['point'].' cho tài khoản '.$user['User']['email'].' ngày ' . date('d-m-Y', time());
                    $this->Cmtaction->save($data['Cmtaction']);
                    
                    if(isset($this->notice['point_succ'])) $this->Session->setFlash($this->notice['point_succ'], 'default', array('class' => 'notification success png_bg'));
                    $this->cancel();exit;
                }
                if(isset($this->notice['not_format_point'])) $this->Session->setFlash($this->notice['not_format_point'], 'default', array('class' => 'notification error png_bg'));
                $this->cancel();exit;
            }
            if(isset($this->notice['not_point'])) $this->Session->setFlash($this->notice['not_point'], 'default', array('class' => 'notification error png_bg'));
            $this->cancel();exit;
        }
        if(isset($this->notice['not_point'])) $this->Session->setFlash($this->notice['not_point'], 'default', array('class' => 'notification error png_bg'));
        $this->cancel();exit;
    }
    
    public function goicmt($id = null){
        $this->loadModel('Cmtaction');
        return $this->Cmtaction->find('all', array(
            'conditions' => array('Cmtaction.foreign_id' => $id),
            'order' => 'Cmtaction.id DESC'
        ));
    }
    
    public function close($id = null) {
        $this->User->id = $id;
        $this->User->saveField('status', '0');
        if(isset($this->notice['close'])) $this->Session->setFlash($this->notice['close'], 'default', array('class' => 'notification success png_bg'));
        $this->cancel();
    }

    public function active($id = null) {
        $this->User->id = $id;
        $this->User->saveField('status', '1');
        if(isset($this->notice['active'])) $this->Session->setFlash($this->notice['active'], 'default', array('class' => 'notification success png_bg'));
        $this->cancel();
    }
}